<template>
  <div class="content-ai">
    <div class="ai-heard">智能问答</div>
    <div class="ai-center" ref="scrollRef">
      <div class="center-item" v-for="(item, index) in list" :key="item.id">
        <div class="AI-user" v-if="item.role === 'user'">
          <img :src="publicUrls + `door/AIuser.png`" alt="" />
          <p class="user-text">{{ item.content }}</p>
        </div>
        <div class="message-box" v-if="item.role === 'assistant'">
          <div class="AI-messagge">
            <img :src="publicUrls + `door/AIsuccess.png`" alt="" />
            <div class="messagge-text">{{ item.content ? item.content : '...' }}</div>
          </div>
          <div class="function-box">
            <span class="function-left" @click="rest(index - 1)">{{ item.content ? '重新生成' : '' }}</span>
            <div class="function-right" @click="copyText(item.content)">
              <img :src="publicUrls + `door/AIcopy.png`" alt="" />
              <span v-if="item.content !== '暂未检索到相关答案'">复制</span>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="ai-bottom">
      <div class="ai-bottom-top-parent">
        <div class="ai-bottom-top">
          <div class="ai-bottom-top-left" @keyup.enter="searchValue">
            <t-textarea
              :disabled="textLock"
              :maxlength="80"
              v-model="AIvalue"
              :placeholder="textLock ? '请等待AI返回结果' : '请输入问题'"
              name="description"
              :autosize="{ minRows: 1, maxRows: 5 }"
            />
          </div>
          <button class="ai-bottom-top-right" v-loading="loading" @click="searchValue">
            <img :src="publicUrls + `door/aiMessage.png`" alt="" />
          </button>
        </div>
      </div>
      <p class="ai-bottom-tip">生成的所有内容均由人工智能模型生成，不代表本平台态度或观点</p>
    </div>
  </div>
</template>

<script setup lang="ts">
// @ts-nocheck
import { aiMessage, getAimessageList, AiClear } from '@/api/ai/ai';
import { publicUrls } from '@/utils/minxin';
import { getPDFexportList, getWordexport } from '@/api/exhibit/exhibit';
// import { exportPdfContext } from '@/utils/getPdfExport';
import { doorUrl } from '@/utils/minxin';
import { useRouter, useRoute } from 'vue-router';
import { pathSquare } from '@/utils/minxin';
import { configData } from '@/api/templates/templates';
import messageModel from '@/components/messageModel/index.vue';
import { MessagePlugin } from 'tdesign-vue-next';
import { urls } from '@/utils/request/upload';
import { addsettings } from '@/api/project/project';
import { reactive, ref, computed, nextTick, onUnmounted, watch } from 'vue';
import { number } from 'echarts/core';
import { useProjectStore } from '@/store';
import { disableSort, moduleDelete, moduleSave, moduleList } from '@/api/desgin/desgin';
import { storeToRefs } from 'pinia';

const urlConcat = ref('');
const store = useProjectStore();
const { AddUnitstatus } = storeToRefs(store);
const AIvalue = ref('');
const list = ref([]);
const loading = ref(false);
const timer = ref(null);
const textLock = ref(false);
const scrollRef = ref(null);

const init = async () => {
  try {
    let { data } = await getAimessageList();
    list.value = data ? data : [];
    toBottom();
  } catch {
    list.value = [];
  }
};
init();
const toBottom = () => {
  nextTick(() => {
    if (scrollRef.value?.scrollHeight) {
      scrollRef.value.scrollTop = scrollRef.value.scrollHeight;
    }
  });
};
const searchValue = async () => {
  if (textLock.value) {
    MessagePlugin.error('请等待上一次回答结束');
  }
  let arr = AIvalue.value.replace(/\s/g, '');
  if (!AIvalue.value || !arr) {
    MessagePlugin.error('请填写检索内容');
    return;
  }
  try {
    let contents = AIvalue.value;
    textLock.value = true;
    list.value.push({ content: contents, role: 'user' });
    AIvalue.value = '';
    list.value.push({ content: '', role: 'assistant' });

    const { data } = await aiMessage(contents);

    getanswer(list.value.length - 1, data);
    loading.value = false;
    toBottom();
  } catch (err) {
    console.log(err);
    loading.value = false;
    textLock.value = false;
  }
};
const getanswer = (index: number, str?: string) => {
  if (!str) {
    list.value[index].content = '暂未检索到相关答案';
    textLock.value = false;
    return;
  }
  let indexs = 0;
  textLock.value = true;
  timer.value = setInterval(() => {
    indexs++;
    // console.log(str.substring(0, indexs));
    list.value[index].content = str.substring(0, indexs);
    toBottom();
    if (indexs === str.length) {
      clearInterval(timer.value);
      textLock.value = false;
    }
  }, 100);
  //   const type = () => {
  //     if (indexs < str.length) {

  //       index++;
  //       setTimeout(type, delay);
  //     }
  //   };
  //   type();
};
const copyText = (str: any) => {
  if (!str) {
    return;
  }
  var cInput = document.createElement('input');
  cInput.value = str;
  document.body.appendChild(cInput);
  cInput.select(); // 选取文本框内容
  document.execCommand('copy');

  // 复制成功后再将构造的标签 移除
  document.body.removeChild(cInput);
  MessagePlugin.success('复制成功');
};
const rest = async (index: number) => {
  // textLock.value = false;

  try {
    if (textLock.value) {
      MessagePlugin.error('请等待上一次回答结束');
      return;
    }
    textLock.value = true;
    list.value.push({ content: list.value[index].content, role: 'user' });
    const { data } = await aiMessage(list.value[index].content);
    list.value.push({ content: '', role: 'assistant' });
    getanswer(list.value.length - 1, data);
    loading.value = false;
    toBottom();
  } catch (err) {
    console.log(err);
    loading.value = false;
    textLock.value = false;
  }
};
</script>

<style lang="less" scoped>
.content-ai {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  height: 100%;
}
.ai-heard {
  display: flex;
  align-items: center;
  justify-content: center;

  height: 28px;
  font-size: 18px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  color: #ff0000;
  line-height: 28px;
  background: linear-gradient(119deg, #12aad0 0%, #8445e3 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 16px;
  text-align: center;
}
.ai-center {
  display: flex;
  flex-direction: column;
  flex: 1;
  width: 100%;
  overflow: auto;
  scrollbar-width: none;
  //   scroll-behavior: smooth;
}
.ai-center::-webkit-scrollbar {
  width: 2px;
  height: 2px;
  scrollbar-width: none;
}
.ai-center::-webkit-scrollbar-track {
  width: 2px;
}
.ai-center::-webkit-slider-thumb {
  width: 2px;
}
.ai-bottom {
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 84px;
  margin-top: 16px;
}
.ai-bottom-top-parent {
  width: 100%;
  border-radius: 8px;

  /* 设置背景颜色为渐变色 */
  background-image: linear-gradient(132deg, rgba(185, 225, 243, 1), rgba(199, 183, 234, 1));

  /* 将背景色限制在边框内 */
  background-clip: padding-box;

  /* 设置边框样式和颜色 */
  padding: 1px;
  border: 0px solid transparent;
}
.ai-bottom-top {
  display: flex;
  align-items: flex-end;
  width: 100%;
  min-height: 64px;
  background: #ffffff;

  // border: 1px solid transparent;
  // box-shadow: 0 0 0 8px linear-gradient(132deg, rgba(185, 225, 243, 1), rgba(199, 183, 234, 1));
  // border-image: linear-gradient(132deg, rgba(185, 225, 243, 1), rgba(199, 183, 234, 1)) 1 1;
  border-radius: 8px;
  padding: 12px;
}
// .ai-bottom-top:hover {
//   // border-image: linear-gradient(132deg, rgba(22, 167, 209, 1), rgba(130, 70, 227, 1)) 1 1;
// }
.ai-bottom-top-parent:hover {
  background-image: linear-gradient(132deg, rgba(22, 167, 209, 1), rgba(130, 70, 227, 1));
}
.ai-bottom-top-left {
  flex: 1;
  min-height: 40px;
  margin-right: 12px;
  :deep(.t-textarea) {
    border: none;
    min-height: 40px;
  }
  :deep(.t-textarea__inner) {
    padding: 0px;
    font-size: 16px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.8);
    line-height: 24px;
    border: none;
  }
}
.ai-bottom-top-right {
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(314deg, #f5effd 0%, #dae5f9 100%);
  border: none;
  img {
    width: 20px;
    height: 20px;
  }
}
.ai-bottom-top-right:hover {
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
}
.ai-bottom-top-right:active {
  background: #dcdff5;
  box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
}
.ai-bottom-tip {
  height: 20px;
  font-size: 13px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.38);
  line-height: 20px;
}
//中心问答样式
.center-item {
  display: flex;
  flex-direction: column;
  width: 100%;
  margin-bottom: 26px;
}
.AI-user {
  display: flex;

  img {
    width: 28px;
    height: 28px;
    margin-right: 8px;
  }
}
.user-text {
  width: calc(100% - 36px);
  font-size: 16px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 500;
  color: rgba(0, 0, 0, 0.8);
  line-height: 24px;
  word-break: break-all !important; /* 允许在单词内任意位置换行 */
  white-space: pre-wrap;
}
.message-box {
  display: flex;
  flex-direction: column;
  width: 100%;
}
.AI-messagge {
  display: flex;
  img {
    width: 28px;
    height: 28px;
    margin-right: 8px;
  }
}
.messagge-text {
  flex: 1;
  background: #ffffff;
  border-radius: 8px;
  padding: 12px 16px;
  font-size: 16px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.59);
  line-height: 28px;
}
.function-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-top: 4px;
  padding-left: 36px;
}
.function-left {
  cursor: pointer;
  font-size: 14px;
  font-family: PingFangSC, PingFang SC;
  font-weight: 400;
  color: #5f67dd;
  line-height: 22px;
}
.function-right {
  cursor: pointer;
  display: flex;
  align-items: center;
  img {
    width: 12px;
    height: 12px;
    margin-right: 6px;
  }
  span {
    font-size: 14px;
    font-family: PingFangSC, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.59);
    line-height: 22px;
  }
}
:deep(.t-textarea .t-is-disabled) {
  background: transparent;
  pointer-events: none;
}
</style>
